<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
error_reporting(E_ALL);
include_once('controller/includes/db_access.php');

function dateformat($param) {
    // format "2012-06-19"  
    $dt = strftime("%Y-%m-%d", strtotime($param));
    // convertir les accents (pour encodage UTF-8)  
    $dt = mb_convert_encoding($dt, 'utf-8');
    return $dt;
}

//1 post datum from stap1
$van_datum = dateformat($_POST['van']);
$tot_datum = dateformat($_POST['tot']);
$set_id = $_POST['set_id'];
$select_allecategorie = mysql_query("SELECT * FROM categorieen C, product P WHERE C.categorie_id = P.categorie_id GROUP BY C.categorie_id");
while ($rows_allecategorie = mysql_fetch_object($select_allecategorie)) {
            
$categorie_id = $rows_allecategorie->categorie_id;

 $SQL_alleproducts = "SELECT * FROM product WHERE categorie_id=".$categorie_id." ORDER BY  huurprijs ASC";
$query_alleproducts = mysql_query($SQL_alleproducts);
 while ($row_alleproducts = mysql_fetch_object($query_alleproducts)) {
     // post producten aantal vanaf stap3////////////////////////////////////////
   if(isset($_POST['product'.$row_alleproducts->product_id])&&$_POST['product'.$row_alleproducts->product_id]>0){
     $product=$row_alleproducts->product_id;
     $aantal_per_product=$_POST['product'.$row_alleproducts->product_id];
     
     }
 }
 
 }
    

//1.1 count aantal dagen
function daysDifference($endDate, $beginDate) {
    //explode the date by "-" and storing to array
    $date_parts1 = explode("-", $beginDate);
    $date_parts2 = explode("-", $endDate);
    //gregoriantojd() Converts a Gregorian date to Julian Day Count
    $start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
    $end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
    return $end_date - $start_date;
}

$total_day = daysDifference($tot_datum, $van_datum);

    
    
// STAP 1 VOOR ELKE DAG VAN DE VERHUUR PERIODE/////////////////////////////////////////////////////////////


$prodcten_beschikbaarheid=array();

$SQL_all_diensten = "SELECT * FROM diensten WHERE eenxdienst='N'";
$query_all_diensten = mysql_query($SQL_all_diensten);
 while ($row_all_diensten = mysql_fetch_object($query_all_diensten)) {
     
     $dienst_id=$row_all_diensten->dienst_id;
    $aantal_voorraad=$row_all_diensten->aantal_per_dag;
   
     $begin_dag = $van_datum;  
     
     for ($i = 1; $i <= $total_day; $i++) {
         
        // CHECK beschikbaarheidstabel 
        $sql_inhuur = "SELECT SUM(aantal) AS totaal, dienst_id FROM beschikbaarheid WHERE datum='$begin_dag' AND dienst_id=" . $dienst_id;
        $result_inhuur = mysql_query($sql_inhuur);
        $rows_inhuur = mysql_fetch_object($result_inhuur);
        
        // bereken hoeveel beschikbaar
        //echo $begin_dag.' | '.$pr_id.' | '.
        $beschikbaar_aantal =$aantal_voorraad-$rows_inhuur->totaal;
        
        if (!array_key_exists($dienst_id,$prodcten_beschikbaarheid)) {
            
           $prodcten_beschikbaarheid[$dienst_id]=$beschikbaar_aantal;
           //echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S1<br />';
        } else {
            
            if ($beschikbaar_aantal<$prodcten_beschikbaarheid[$dienst_id]) {
                $prodcten_beschikbaarheid[$dienst_id]=$beschikbaar_aantal;
               // echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S2<br />';
            } else {
                //echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S3<br />';
            }
        }
         
         $date = strtotime(date("Y-m-d", strtotime($begin_dag)) . " +1 day");
         $begin_dag = date('Y-m-d', $date);
        
        
     }
     
  
 }
 
 // STAP2 VOOR EEN MALIGE VAN DE VERHUUR PERIODE///////////////////////////////////////////////////////////////////////////////////

$SQL_all_diensten_STAP2 = "SELECT * FROM diensten WHERE eenxdienst='Y'";
$query_all_diensten_STAP2 = mysql_query($SQL_all_diensten_STAP2);
 while ($row_all_diensten_STAP2 = mysql_fetch_object($query_all_diensten_STAP2)) {
     
     $dienst_id_STAP2=$row_all_diensten_STAP2->dienst_id;
    $aantal_voorraad_STAP2=$row_all_diensten_STAP2->aantal_per_dag;
   
     $dag = $van_datum;  
     
     for ($i = 1; $i <= 2; $i++) {
         
        // CHECK beschikbaarheidstabel 
        $sql_inhuur_STAP2 = "SELECT SUM(aantal) AS totaal, dienst_id FROM beschikbaarheid WHERE datum='$dag' AND dienst_id=" . $dienst_id_STAP2;
        $result_inhuur_STAP2 = mysql_query($sql_inhuur_STAP2);
        $rows_inhuur_STAP2 = mysql_fetch_object($result_inhuur_STAP2);
        
        // bereken hoeveel beschikbaar
        //echo $begin_dag.' | '.$pr_id.' | '.
        $beschikbaar_aantal_STAP2 =$aantal_voorraad_STAP2-$rows_inhuur_STAP2->totaal;
        
        if (!array_key_exists($dienst_id_STAP2,$prodcten_beschikbaarheid)) {
            
           $prodcten_beschikbaarheid[$dienst_id_STAP2]=$beschikbaar_aantal_STAP2;
           //echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S1<br />';
        } else {
            
            if ($beschikbaar_aantal_STAP2<$prodcten_beschikbaarheid[$dienst_id_STAP2]) {
                $prodcten_beschikbaarheid[$dienst_id_STAP2]=$beschikbaar_aantal_STAP2;
               // echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S2<br />';
            } else {
                //echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S3<br />';
            }
        }
         
        
         $dag = $tot_datum;
        
        
     }
     
  
 }
 //print_r($prodcten_beschikbaarheid);
 
 
 // PAGE CONTENT
 ?>
<form name="form1" action="stap5.php" method="post">  
 <input type="hidden" name="van" value="<?php echo $_POST['van']; ?>" />
 <input type="hidden" name="tot" value="<?php echo $_POST['tot']; ?>" />
 <input type="hidden" name="set_id" value="<?php echo $_POST['set_id']; ?>" />
   <input type="hidden" name="dt" value="<?php echo $_POST['dt']; ?>" />
       <input type="hidden" name="aantal_dag" value="<?php echo $_POST['aantal']; ?>" />
 <?php $select_allecategorie = mysql_query("SELECT * FROM categorieen C, product P WHERE C.categorie_id = P.categorie_id GROUP BY C.categorie_id");
while ($rows_allecategorie = mysql_fetch_object($select_allecategorie)) {         
$categorie_id = $rows_allecategorie->categorie_id;

$SQL_alleproducts = "SELECT * FROM product WHERE categorie_id=".$categorie_id." ORDER BY  huurprijs ASC";
$query_alleproducts = mysql_query($SQL_alleproducts);
 while ($row_alleproducts = mysql_fetch_object($query_alleproducts)) {
     // post producten aantal vanaf stap3////////////////////////////////////////
   if(isset($_POST['product'.$row_alleproducts->product_id]) && (($_POST['product'.$row_alleproducts->product_id])>0 )){

     $aantal_per_product=$_POST['product'.$row_alleproducts->product_id];
     echo '<input type="hidden" name="product[]" value="'.$row_alleproducts->product_id.'" />';
     echo '<input type="hidden" name="aantal[]" value="'.$_POST['product'.$row_alleproducts->product_id].'" />';
     }
 }
 
 }?>
<table border="0" cellspacing="5" cellpadding="5">
    <thead>
        <tr>
            <th colspan="3"><h1>stap4 Diensten</h1></th>

</tr>
</thead>
<tbody>
 <?php
 $SQL_alleproducts = "SELECT * FROM diensten ORDER BY prijs ASC";
$query_alleproducts = mysql_query($SQL_alleproducts);
 while ($row_alleproducts = mysql_fetch_object($query_alleproducts)) {
 ?>    
    
<tr>
    <td colspan="2"><input type="checkbox" name="dienst[]" value="<?php echo $row_alleproducts->dienst_id ?>"  <?php echo ((($prodcten_beschikbaarheid[$row_alleproducts->dienst_id])<=0) && ($total_day = 0 )) ? 'disabled="disabled"' : ''; ?>   
    <?php echo $row_alleproducts->altijd_toepassen == 'Y' ? ' disabled="disabled" checked="CHECKED"' : ''; ?>/>
    <?php echo $row_alleproducts->altijd_toepassen == 'Y' ? '<input type="hidden" name="dienst[]" value="'. $row_alleproducts->dienst_id .'">' : ''; ?>

         <?php echo $row_alleproducts->naam; ?> 
         <?php if ((($prodcten_beschikbaarheid[$row_alleproducts->dienst_id])<=0) && ($total_day = 0 )) echo '<font color="red">Niet beschikbaar</font>'; ?>
          </td>
            <td> <?php  echo ((($prodcten_beschikbaarheid[$row_alleproducts->dienst_id])<=0) && ($total_day = 0 )) ? '<font color="#ccc"> prijs: &euro;'.$row_alleproduct->prijs.'</font>':' prijs: &euro;'.$row_alleproducts->prijs; ?> </td>
        </tr>
     
 <?php    
 }

 
?>
<tr>
        <td></td>
        <td><input type="submit" value="volgende stap"/></td>
        <td></td>
    </tr>
</tbody>
</table></form> 